package com.test.springboot3.springboot3testdemo1.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.SecurityFilterChain;

/**
 * Created with IntelliJ IDEA.
 *
 * @author： liuziyang
 * @date： 2024/8/2-下午5:21
 * @description：
 * @modifiedBy：
 * @version: 1.0
 */
@Configuration
public class SecurityConfig {
  @Bean
  public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
    /*return http.authorizeHttpRequests(
    authorize -> {
      authorize
          .requestMatchers("/test/**")
          .hasRole("test")
          */
    /*.requestMatchers(EndpointRequest.to("health"))
    .hasRole("admin")*/
    /*
              .requestMatchers("/**")
              .permitAll();
        })
    .logout(withDefaults())
    .formLogin(withDefaults())
    .csrf(csrf -> csrf.disable())
    .build();*/

    return http.authorizeHttpRequests(authorize -> authorize.anyRequest().permitAll())
        .csrf(csrf -> csrf.disable())
        .build();
  }

  @Bean
  public UserDetailsService userDetailsService() {
    InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
    manager.createUser(
        User.withDefaultPasswordEncoder()
            .username("user")
            .password("123456")
            .roles("test", "admin")
            .build());
    return manager;
  }
}
